package com.zd.ticketing.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.bean.model.TicketOrder;
import com.zd.common.bean.model.UserInfo;
import com.zd.common.common.enums.PayStatusEnum;
import com.zd.common.common.utils.LogUtils;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.common.utils.WeixinApiUtils;
import com.zd.ticketing.bean.vo.ScenicSpotDetailVo;
import com.zd.ticketing.bean.vo.TicketOrderDetailVo;
import com.zd.ticketing.bean.vo.TicketOrderVo;
import com.zd.ticketing.service.IScenicSpotService;
import com.zd.ticketing.service.ITicketInfoService;
import com.zd.ticketing.service.ITicketOrderService;
import com.zd.ticketing.service.IUserInfoService;

@Controller
@RequestMapping("order")
public class OrderController {
	
	@Autowired
	private IScenicSpotService scenicSpotService;
	
	@Autowired
	private IUserInfoService userInfoService;
	
	@Autowired
	private ITicketOrderService ticketOrderService;
	
	@Autowired
	private ITicketInfoService ticketInfoService;

	
	@RequestMapping("/orderWaiting")
	public ModelAndView orderWaiting(Integer userId)throws Exception{
		ModelAndView mv = new ModelAndView();
		mv.addObject("userId", userId);
		mv.setViewName("order/waiting");
		return mv;
	}
	
	
	@RequestMapping("/myOrder")
	public ModelAndView myOrder(String appId, String openId, String nickname ,String img,Integer userId) throws Exception{
		ModelAndView mv = new ModelAndView();
		UserInfo userInfo = new UserInfo();
		Map<String, Object> resultMap = new HashMap<>();
		if(userId == null){
			ScenicSpotDetailVo scenicSpotDetailVo = scenicSpotService.getScenicSpotDetail(appId);
			userInfo = userInfoService.findOpenId(scenicSpotDetailVo.getId(), openId, nickname, img);
			resultMap = userInfoService.findUserOrder(userInfo.getId());
			mv.addObject("userId", userInfo.getId());
		}else {
			resultMap = userInfoService.findUserOrder(userId);
			mv.addObject("userId", userId);
		}
		mv.addObject("orderMap", resultMap);
		LogUtils.info(resultMap);
		mv.setViewName("order/myOrder");
		return mv;
	}
	
	@RequestMapping("paymentSuccessOrder")
	@ResponseBody
	public Map<String, Object> getPaymentSuccessOrder(Integer page , Integer userId)throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		List<TicketOrderVo> ticketOrderVoList = userInfoService.pageTicketOrder(page, userId);
		resultMap.put("ticketOrderVoList", ticketOrderVoList);
		return resultMap;
	}
	
	@RequestMapping("singleOrder")
	public ModelAndView toSingleOrder(Integer orderId , HttpServletRequest request)throws Exception{
		ModelAndView mv = new ModelAndView();
		String url = request.getRequestURL().toString();
		String queryStr = request.getQueryString();
		if (!StringUtils.isBank(queryStr)) {
			url += "?" + queryStr;
		}
		
		List<TicketOrderDetailVo> ticketOrderDetailVoList = ticketOrderService.getTicketOrderDetailList(orderId);
		if(ticketOrderDetailVoList == null || ticketOrderDetailVoList.size() ==0){
			mv.setViewName("order/noCompletedOrder");
			return mv;
		}
		TicketOrder ticketOrder = ticketOrderService.get(orderId);
		if(ticketOrder.getStatus() == PayStatusEnum.Payment_success.getKey()){
			mv.setViewName("order/completedOrder");
		}else {
			ScenicSpotDetailVo scenicSpotDetailVo = scenicSpotService.getScenicSpotDetail(ticketOrder.getScenicSpotId());
			JSONObject signatureObj = WeixinApiUtils.jssdk_signture(url, scenicSpotDetailVo.getPayAccountId());
			mv.addObject("signature", signatureObj);
			mv.setViewName("order/noCompletedOrder");
		}
		TicketInfo ticketInfo = ticketInfoService.get(ticketOrder.getTicketId());
		mv.addObject("ticketInfo", ticketInfo);
		mv.addObject("ticketOrder", ticketOrder);
		mv.addObject("ticketOrderDetailVoList", ticketOrderDetailVoList);
		return mv;
	}
	
	@RequestMapping("/cancel")
	@ResponseBody
	public Map<String, String> cancelOrder(Integer orderId)throws Exception {
		 Map<String, String> resultMap = new HashMap<>();
		 resultMap = ticketOrderService.cancelOrder(orderId);
		 return resultMap;
	}
	 
}
